#include <iostream>
using namespace std;
#include "../source/public.h"

void test_socket()
{
    string str = "www.baidu.com";
    framework::IPAddress::ptr addr = framework::Address::LookupAnyIPAddress("www.baidu.com");

    if(addr)
    {
        cout << "get addr: " << addr->toString() << endl;
    }   
    else
    {
        cout << "get addr fail" << endl;
        return;
    }

    framework::Socket::ptr sock = framework::Socket::CreateTCP(addr);
    addr->setPort(80);
    cout << "addr: " << addr->toString() << endl;
    if(!sock->connect(addr))
    {
        cout << "connect error" << endl;
        return;
    }

    const char buff[] = "GET / HTTP/1.0\r\n\r\n";
    int rt = sock->send(buff, sizeof(buff));
    if(rt <= 0)
    {
        cout << "send fail rt=" << rt;
        return;
    }

    std::string buffer;
    buffer.resize(4096);
    cout << "buffer capacity: " << buffer.capacity() << endl;
    cout << "buffer size: " << buffer.size() << endl;

    cout << "================================================================\n";
    rt = sock->recv(&buffer[0],buffer.size());
    if(rt <= 0)
    {
        cout << "send fail rt=" << rt;
        return;
    }
    buffer.resize(rt);
    cout << "re :" << rt << endl;
    cout << "buffer capacity: " << buffer.capacity() << endl;
    cout << "buffer size: " << buffer.size() << endl;
    cout << buffer << endl;

}

int main(void)
{
    framework::IOManager iom;
    iom.schedule(&test_socket);

    return 0;
}